<?php
/**
 * 奖惩管理
 *  
 */ 
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class JiangchengController extends BaseController {

	//人事惩罚列表
	public function getHrPunishData(){
		$yggh = I('get.yggh','','intval');
		$name = I('get.name','','trim');
		$sector = I('get.sector','','intval');
		$punishdateA = I('get.punishdateA','','trim');
		$punishdateB = I('get.punishdateB','','trim');
		$type = I('get.type','','intval');
		$class = I('get.class','','intval');
		$psize = I('get.psize', $this->m_psize,'intval');
		$psize = (!$psize || $psize>20 ) ? $this->m_psize:$psize ;

		$wheres = array();
		$wheres['p.fine_type'] = array('eq', 3);

		if (strlen($yggh) > 0 && is_numeric($yggh) && $yggh > 0) {
			$wheres['ue.ext_yggh'] = array('eq', $yggh);
		}
		if (strlen($name) > 0) {
			$wheres['ue.ext_name'] = array('like','%'.$name.'%');
		}
		if (strlen($sector) > 0 && is_numeric($sector) && $sector > 0) {
			$wheres['us.sector_id'] = array('in', $this->getsubSector( $sector ));
		}
		if( $punishdateA && $punishdateB){
			$tmA = strtotime( $punishdateA );
			$tmB = strtotime( $punishdateB );
			$stimeA = date('Y-m-d', $tmA );
			$stimeB = date('Y-m-d', $tmB );
			if ($stimeA < $stimeB) {
				$wheres['p.pub_time'] = array('between', array($stimeA, $stimeB));
			}else{
				$wheres['p.pub_time'] = array('between', array($stimeB, $stimeA));
			}			
		}else{
			if ($punishdateA) {
				$tm = strtotime( $punishdateA );
				$stime = date('Y-m-d', $tm );
				$wheres['p.pub_time'] = array('egt', $stime);
			}
			if ($punishdateB) {
				$tm = strtotime( $punishdateB );
				$stime = date('Y-m-d', $tm );
				$wheres['p.pub_time'] = array('elt', $stime);
			}
		}

		if (strlen($type) > 0 && is_numeric($type) && $type >= 0) {
			$wheres['p.hr_fine_type'] = array('eq', $type);
		}
		if (strlen($class) > 0 && is_numeric($class) && $class >= 0) {
			$wheres['pu.fine_type'] = array('eq', $class);
		}

		$fineuser = M("Fine_user");
		$wheres['_string'] = "1=1";
		$cnt =$fineuser->table('fine_user as pu')
						->join("left join fine as p on p.fine_id  = pu.fine_id")
					   	->join("left join user_extension as ue on ue.user_id  = pu.sales_id")
					   	->join("left join user as us on us.user_id = pu.sales_id")
					   	->join('left join sector as se on se.sector_id = us.sector_id')
					   	->join('left join role as ro on ro.role_id = us.role_id')
					   	->where( $wheres )
					   	->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $fineuser->field("ue.ext_name,ue.ext_yggh,se.sector_name,ro.role_name,p.pub_time,p.description,pu.fine_id,pu.sales_id,pu.fine_remark,ue.ext_user_status")
					   	->table('fine_user as pu')
					   	->join("left join fine as p on p.fine_id  = pu.fine_id")
					   	->join("left join user_extension as ue on ue.user_id  = pu.sales_id")
					   	->join("left join user as us on us.user_id = pu.sales_id")
					   	->join('left join sector as se on se.sector_id = us.sector_id')
					   	->join('left join role as ro on ro.role_id = us.role_id')
					   	->where( $wheres )
					   	->order("p.pub_time desc ")
					   	->limit($Page->firstRow.','.$Page->listRows)
					   	->select();
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $this->chgetData($dlist, 1); //$dlist; //
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "内容为空";
		}
		e_json( $res );
	}

	public function getsubSector( $pid ){
		$strList = array();
		if ( $pid > 0 ) {
			$sector = D('Sector');
			$strList = $sector->sub_sector($pid);
		}
		return $strList;
	}

	private function chgetData($data, $icom = 0){
		foreach ($data as $key => $value) {
			//补全员工工号六位数
			$yggh = $value['ext_yggh'];
			$ylen = strlen($yggh);
			if ($ylen < 6) {
				$strpre = '';
				for ($i=0; $i < 6 - $ylen; $i++) { 
					$strpre .= '0';
				}
				$data[$key]['ext_yggh'] = $strpre.$data[$key]['ext_yggh'];
			}
			//在职状态
			switch ($value['ext_user_status']) {
				case 0:
					$data[$key]['ext_user_status'] = '正常';
					break;
				case 1:
					$data[$key]['ext_user_status'] = '产假';
					break;
				case 2:
					$data[$key]['ext_user_status'] = '停薪留职';
					break;
				case 3:
					$data[$key]['ext_user_status'] = '停职留薪';
					break;
				default:
					$data[$key]['ext_user_status'] = '尚未确定';
					break;
			}
			//个性化$icom
			if ($icom == 1 ) {
				if (strlen($value['pub_time']) > 10) {
					$data[$key]['pub_time'] = substr($value['pub_time'], 0, 10);
				}
			}

		}
		return $data;
	}

	//搜索用户
	public function getUserList(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$strkey = I('get.key','','trim');
		if ( $strkey && strlen($strkey) > 0 ) {
			$where = array();
			$where['ue.ext_name']  = array('like', '%'.$strkey.'%');
			$where['ue.ext_yggh']  = array('like', '%'.$strkey.'%');
			$where['u.nickname']  =  array('like', '%'.$strkey.'%');
			$where['_logic'] = 'or';
			$where_x['_complex'] = $where;
			$where_x['u.state']  = array('neq', -1 );
			$user = M('User');
			$udata = $user->field('u.user_id,u.login_name,u.sector_id,u.role_id,s.sector_name,r.role_name,ue.ext_name,ue.ext_user_status')
						->table('user as u')
						->join('left join sector as s on s.sector_id = u.sector_id')
						->join('left join role as r on r.role_id = u.role_id')
						->join('left join user_extension as ue on ue.user_id = u.user_id')
						->where( $where_x )
						->limit(10)
						->select();
			if( $udata ){
				$res['rcode'] = 1;
				$res['data'] = $this->getUserListHelp($udata);
				$res['msg'] = "ok";
			}else{
				$res['rcode'] = 2;
				$res['msg'] = "内容为空";
			}
		}
		e_json( $res );
	}

	private function getUserListHelp($data){
		foreach ($data as $key => $value) {
			$data[$key]['stridlist'] = $data[$key]['user_id'].'_'.$data[$key]['sector_id'].'_'.$data[$key]['role_id'].'_'.$data[$key]['ext_user_status'];
			$data[$key]['strvaluelist'] = $data[$key]['login_name'].'-'.$data[$key]['ext_name'].'-'.$data[$key]['sector_name'].'-'.$data[$key]['role_name'];
		}
		return $data;
	}

	//添加惩罚记录
	public function addPunish(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data['user_id'] = I('post.seluser','','trim');
		$data['punish_date'] = I('post.punishdate','','trim');
		$data['punish_type'] = I('post.type','','intval');
		$data['punish_reason'] = I('post.note','','trim');
		$data['punish_class'] = I('post.class','','intval');
		$data['punish_clsay'] = I('post.clsay','','trim');
		$data['punist_beizhu'] = I('post.beizhu','','trim');
		$fine = D('Fine');
		$addmsg = $fine->hrdadd( $data , $this->m_user['user_id']);
		$res['rcode'] = $addmsg['rcode'];
		$res['msg'] = $addmsg['msg'];
		e_json( $res );
	}

	//编辑惩罚记录
	public function editPunish(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data = I('post.');
		$fine = D('Fine');
		$editmsg = $fine->hrdsave( $data );
		$res['rcode'] = $editmsg['rcode'];
		$res['msg'] = $editmsg['msg'];
		e_json( $res );
	}

	//是检查是否为事件最后一条记录
	public function chenkOnerecord(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$delfid = I('post.fid','','intval');
		$fineuser = D('Fine_user');
		$gmsg = $fineuser->checkOne( $delfid );
		$res['rcode'] = $gmsg['rcode'];
		e_json( $res );
	}

	//删除惩罚记录
	public function delPunish(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$delfid = I('post.fid','','intval');
		$deluid = I('post.uid','','intval');
		$fineuser = D('Fine_user');
		$delmsg = $fineuser->ddelete( $delfid , $deluid );
		$res['rcode'] = $delmsg['rcode'];
		$res['msg'] = $delmsg['msg'];
		e_json( $res );
	}

	//获取处罚表数据by id
	public function editorPunishInfoById(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$editid = I('get.id', '', 'intval');
		$where = array();
		if ( $editid && is_numeric($editid) && $editid > 0 ) {
			$where['p.fine_id'] = array('eq', $editid);
			$fine = M('Fine');
			$edata = $fine->field('p.fine_id,p.pub_time,p.hr_fine_type,p.description,p.remark,pu.sales_id,pu.fine_type,pu.fine_remark')
						->table('fine as p')
						->join('left join fine_user as pu on pu.fine_id = p.fine_id')
						->where( $where )->find();
			if ($edata) {
				$res['rcode'] = 1;
				$res['data'] = $edata;
				$res['msg'] = 'ok';
			}
		}
		e_json( $res );
	}

	//----------------------------------------------------嘉奖
	//嘉奖列表
	public function getHrCommendData(){
		$yggh = I('get.yggh','','intval');
		$name = I('get.name','','trim');
		$sector = I('get.sector','','intval');
		$commdateA = I('get.commdateA','','trim');
		$commdateB = I('get.commdateB','','trim');
		$type = I('get.type','','intval');
		$class = I('get.class','','intval');
		$psize = I('get.psize', $this->m_psize,'intval');
		$psize = (!$psize || $psize>20 ) ? $this->m_psize:$psize ;

		$wheres = array();
		if (strlen($yggh) > 0 && is_numeric($yggh) && $yggh > 0) {
			$wheres['ue.ext_yggh'] = array('eq', $yggh);
		}
		if (strlen($name) > 0) {
			$wheres['ue.ext_name'] = array('like','%'.$name.'%');
		}
		if (strlen($sector) > 0 && is_numeric($sector) && $sector > 0) {
			$wheres['us.sector_id'] = array('in', $this->getsubSector( $sector ));
		}
		if( $commdateA && $commdateB){
			$tmA = strtotime( $commdateA );
			$tmB = strtotime( $commdateB );
			$stimeA = date('Y-m-d', $tmA );
			$stimeB = date('Y-m-d', $tmB );
			if ($stimeA < $stimeB) {
				$wheres['p.comm_date'] = array('between', array($stimeA, $stimeB));
			}else{
				$wheres['p.comm_date'] = array('between', array($stimeB, $stimeA));
			}			
		}else{
			if ($commdateA) {
				$tm = strtotime( $commdateA );
				$stime = date('Y-m-d', $tm );
				$wheres['p.comm_date'] = array('egt', $stime);
			}
			if ($commdateB) {
				$tm = strtotime( $commdateB );
				$stime = date('Y-m-d', $tm );
				$wheres['p.comm_date'] = array('elt', $stime);
			}
		}
		if (strlen($type) > 0 && is_numeric($type) && $type >= 0) {
			$wheres['p.comm_type'] = array('eq', $type);
		}
		if (strlen($class) > 0 && is_numeric($class) && $class >= 0) {
			$wheres['p.comm_class'] = array('eq', $class);
		}

		$hcommend = M("Hr_commend");
		$wheres['_string'] = "1=1";
		$cnt =$hcommend->table('Hr_commend as p')
					   ->join("left join user_extension as ue on ue.user_id  = p.user_id")
					   ->join("left join user as us on us.user_id = p.user_id")
					   ->join('left join sector as se on se.sector_id = us.sector_id')
					   ->where( $wheres )
					   ->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $hcommend->field("ue.ext_name,ue.ext_yggh,se.sector_name,ro.role_name,p.comm_id,p.comm_date,p.comm_reason,p.comm_clsay,ue.ext_user_status")
					   ->table('Hr_commend as p')
					   ->join("left join user_extension as ue on ue.user_id  = p.user_id")
					   ->join("left join user as us on us.user_id = p.user_id")
					   ->join('left join sector as se on se.sector_id = us.sector_id')
					   ->join('left join role as ro on ro.role_id = us.role_id')
					   ->where( $wheres )
					   ->order("p.comm_date desc ")
					   ->limit($Page->firstRow.','.$Page->listRows)
					   ->select();
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $this->chgetData($dlist);
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "内容为空";
		}
		e_json( $res );
	}

	//添加嘉奖记录
	public function addComm(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data = I('post.');
		$hcommend = D('Hr_commend');
		$addmsg = $hcommend->dadd( $data );
		$res['rcode'] = $addmsg['rcode'];
		$res['msg'] = $addmsg['msg'];
		e_json( $res );
	}

	//编辑嘉奖记录
	public function editComm(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data = I('post.');
		$hcommend = D('Hr_commend');
		$editmsg = $hcommend->dsave( $data );
		$res['rcode'] = $editmsg['rcode'];
		$res['msg'] = $editmsg['msg'];
		e_json( $res );
	}

	//删除嘉奖记录
	public function delComm(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$delid = I('post.id','','intval');
		$hcommend = D('Hr_commend');
		$delmsg = $hcommend->ddelete( $delid );
		$res['rcode'] = $delmsg['rcode'];
		$res['msg'] = $delmsg['msg'];
		e_json( $res );
	}

	//获取嘉奖表数据by id
	public function editorCommInfoById(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$editid = I('get.id', '', 'intval');
		$where = array();
		if ( $editid && is_numeric($editid) && $editid > 0 ) {
			$where['comm_id'] = array('eq', $editid);
			$hcommend = M('Hr_commend');
			$edata = $hcommend->field('comm_id,comm_date,comm_type,comm_reason,comm_class,comm_clsay,comm_beizhu')
							->where( $where )->find();
			if ($edata) {
				$res['rcode'] = 1;
				$res['data'] = $edata;
				$res['msg'] = 'ok';
			}
		}
		e_json( $res );
	}
	//----------------------------------------------------特殊贡献
	//特殊贡献列表
	public function getHrOfferData(){
		$yggh = I('get.yggh','','intval');
		$name = I('get.name','','trim');
		$sector = I('get.sector','','intval');
		$ofdateA = I('get.ofdateA','','trim');
		$ofdateB = I('get.ofdateB','','trim');
		$psize = I('get.psize', $this->m_psize,'intval');
		$psize = (!$psize || $psize>20 ) ? $this->m_psize:$psize ;

		$wheres = array();
		if (strlen($yggh) > 0 && is_numeric($yggh) && $yggh > 0) {
			$wheres['ue.ext_yggh'] = array('eq', $yggh);
		}
		if (strlen($name) > 0) {
			$wheres['ue.ext_name'] = array('like','%'.$name.'%');
		}
		if (strlen($sector) > 0 && is_numeric($sector) && $sector > 0) {
			$wheres['us.sector_id'] = array('in', $this->getsubSector( $sector ));
		}
		if( $ofdateA && $ofdateB){
			$tmA = strtotime( $ofdateA );
			$tmB = strtotime( $ofdateB );
			$stimeA = date('Y-m-d', $tmA );
			$stimeB = date('Y-m-d', $tmB );
			if ($stimeA < $stimeB) {
				$wheres['p.of_date'] = array('between', array($stimeA, $stimeB));
			}else{
				$wheres['p.of_date'] = array('between', array($stimeB, $stimeA));
			}			
		}else{
			if ($ofdateA) {
				$tm = strtotime( $ofdateA );
				$stime = date('Y-m-d', $tm );
				$wheres['p.of_date'] = array('egt', $stime);
			}
			if ($ofdateB) {
				$tm = strtotime( $ofdateB );
				$stime = date('Y-m-d', $tm );
				$wheres['p.of_date'] = array('elt', $stime);
			}
		}
		$hoffer = M("Hr_offer");
		$wheres['_string'] = "1=1";
		$cnt =$hoffer->table('Hr_offer as p')
					   ->join("left join user_extension as ue on ue.user_id  = p.user_id")
					   ->join("left join user as us on us.user_id = p.user_id")
					   ->join('left join sector as se on se.sector_id = us.sector_id')
					   ->where( $wheres )
					   ->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $hoffer->field("ue.ext_name,ue.ext_yggh,se.sector_name,ro.role_name,p.of_id,p.of_date,p.of_desc,p.of_get,ue.ext_user_status")
					   ->table('Hr_offer as p')
					   ->join("left join user_extension as ue on ue.user_id  = p.user_id")
					   ->join("left join user as us on us.user_id = p.user_id")
					   ->join('left join sector as se on se.sector_id = us.sector_id')
					   ->join('left join role as ro on ro.role_id = us.role_id')
					   ->where( $wheres )
					   ->order("p.of_date desc ")
					   ->limit($Page->firstRow.','.$Page->listRows)
					   ->select();
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $this->chgetData($dlist);
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "内容为空";
		}
		e_json( $res );
	}

	//添加记录
	public function addOffer(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data = I('post.');
		$hoffer = D('Hr_offer');
		$addmsg = $hoffer->dadd( $data );
		$res['rcode'] = $addmsg['rcode'];
		$res['msg'] = $addmsg['msg'];
		e_json( $res );
	}

	//编辑记录
	public function editOffer(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$data = array();
		$data = I('post.');
		$hoffer = D('Hr_offer');
		$editmsg = $hoffer->dsave( $data );
		$res['rcode'] = $editmsg['rcode'];
		$res['msg'] = $editmsg['msg'];
		e_json( $res );
	}

	//删除记录
	public function delOffer(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$delid = I('post.id','','intval');
		$hoffer = D('Hr_offer');
		$delmsg = $hoffer->ddelete( $delid );
		$res['rcode'] = $delmsg['rcode'];
		$res['msg'] = $delmsg['msg'];
		e_json( $res );
	}

	//获取数据by id
	public function editorOfferInfoById(){
		$res = array('rcode'=>0,'data'=>null, 'msg'=> null);
		$editid = I('get.id', '', 'intval');
		$where = array();
		if ( $editid && is_numeric($editid) && $editid > 0 ) {
			$where['of_id'] = array('eq', $editid);
			$hoffer = M('Hr_offer');
			$edata = $hoffer->field('of_id,of_date,of_desc,of_get,of_beizhu')
							->where( $where )->find();
			if ($edata) {
				$res['rcode'] = 1;
				$res['data'] = $edata;
				$res['msg'] = 'ok';
			}
		}
		e_json( $res );
	}

}
